﻿using Common.Contract.Authentication.Record;
using Common.Contract.Paging;
using Common.Contract.ProductHandbook.Record;
using Common.Contract.ProductHandbook.Request;
using Common.Contract.ProductHandbook.Response;
using Common.Contract.SystemConfig.Record;
using Common.Service.Command;
using CrossSelling.Model.Interface;
using CrossSelling.Model.Model;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;

namespace CrossSelling.Core.ServiceCommand
{
    public class RetrieveChecklistQuestionByProductServiceCommand : ProviderServiceCommand<IProviderFactory, RetrieveChecklistQuestionByProductRequest, RetrieveChecklistQuestionByProductResponse>
    {
        #region Properties
        public UserRecord Dao;
        public FilterRecord TextSearch;
        public int pageSize = 10, pageIndex = 0, totalRecord = 0;
        public int Product;
        #endregion
        public RetrieveChecklistQuestionByProductServiceCommand(IProviderFactory providers) : base(providers) { }
        protected override void Validate(RetrieveChecklistQuestionByProductRequest request)
        {
            if (request == null) throw new ArgumentNullException("request");
            if (!request.Header.ApplicationKey.HasValue || request.Header.ApplicationKey == Guid.Empty)
            {
                throw new ArgumentException("ApplicationKey must be set", "request");
            }
            if (string.IsNullOrEmpty(request.ProductId))
            {
                throw new ArgumentException("Product must be set", "request");
            }
            else
            {
                Int32.TryParse(request.ProductId, out Product);
            }

            if (request.CurrentUser == null)
            {
                throw new ArgumentException("MSBO Dao must be set", "request");
            }
            else
            {
                Dao = Request.CurrentUser;
            }
            TextSearch = Request.Filter;
            if (Request.Page != null)
            {
                pageIndex = Request.Page.PageIndex ?? 0;
                pageSize = Request.Page.PageSize ?? 0;
            }
        }

        protected override bool OnExecute(RetrieveChecklistQuestionByProductResponse response)
        {
            bool isSuccess = true;
            string errorMessage = string.Empty;
            try
            {
                List<ChecklistQuestionRecord> records = new List<ChecklistQuestionRecord>();

                List<ChecklistQuestion> listCustomer = new List<ChecklistQuestion>();
                listCustomer = Providers.ProductHandbooks.RetrieveListQuestionChecklist(out totalRecord, Product, Dao, TextSearch, Request.Lang, pageSize, pageIndex);
                if (listCustomer != null)
                {
                    records = listCustomer.Select(x => new ChecklistQuestionRecord
                    {
                        QuestionId = x.QuestionId,
                        Description = x.Description,
                        ProductId = x.ProductId,
                        Status = x.Status,
                        Title = x.Title,
                        TitleEn = x.TitleEn
                    }).ToList();
                }

                List<ChecklistSubProduct> ListSubProduct = new List<ChecklistSubProduct>();
                ListSubProduct = Providers.ProductHandbooks.RetrieveSubProductWithoutSubId(Product);
                response.ListSubProduct = ListSubProduct.Select(x => new ChecklistSubProductRecord
                {
                    SubProductId = x.SubProductId,
                    SubProductName = x.SubProductName,
                    SubProductNameEn = x.SubProductNameEn
                }).ToList();
                response.Records = records;
                response.Page = new PageRecord
                {
                    PageIndex = pageIndex,
                    PageSize = pageSize,
                    TotalRecords = totalRecord
                };
            }
            catch (Exception e)
            {
                isSuccess = false;
                errorMessage = e.Message;
            }
            response.ErrorMessage = errorMessage;
            return isSuccess;
        }
    }
}
